public class Selection {
    public static void sort(Comparable[] a){
        //将a[]按升序排序
        int N = a.length;
        for(int i = 0; i<N ; i++){
            //将a[i]和a[i+1..N]中最小的元素交换
            int min = i;
            for(int j = i+1;j<N;j++){
                if(less(a[j],a[min]))min = j;
                exch(a,i,min);
            }
        }
    }
    private static boolean less(Comparable v,Comparable w){   //对比大小
        return v.compareTo(w)<0;
    }
    public static void exch(Comparable[] a,int i,int j){   //进行交换
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    private static void show(Comparable[] a){   //显示每次交换情况
        for(int i = 0 ;i<a.length;i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }
    public static boolean isSorted(Comparable[] a){    //判断是否交换
        for(int i = 1;i<a.length;i++)
            if(less(a[i],a[i-1]))
                return false;
        return true;
    }
}
